package com.example.controller.administractor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.UserDTO;
import com.example.domain.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AdministratorUserController {

	private final UserService userService;

	@Autowired
	public AdministratorUserController(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 查询所有用户
	 *
	 * @param currentPage 页号
	 * @param pageSize    每页数量
	 * @return 返回所有用户
	 */
	@PostMapping(value = "/administrator/user/pagination", produces = "application/json;charset=utf-8")
	public String getAllUser(Integer currentPage, Integer pageSize) {
		UserDTO userDTO = new UserDTO();
		userDTO.setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reUserStateInfo(userDTO);
	}

	/**
	 * 修改用户状态
	 *
	 * @param id                 用户id
	 * @param auditRegistryState 审核状态
	 * @param enableState        是否禁用
	 * @return 返回修改是否成功
	 */
	@PostMapping(value = "/administrator/user/update", produces = "application/json;charset=utf-8")
	public String updateUserState(Integer id, Integer auditRegistryState, Integer enableState) {
		Map<String, Object> message = new HashMap<>(16);
		User user = userService.getById(id);
		user.setAuditRegistryState(auditRegistryState);
		user.setEnableState(enableState);
		boolean b = userService.updateById(user);
		message.put("information", b ? 1 : 0);
		return JSON.toJSONString(message);
	}

	/**
	 * 根据telephone查询用户
	 *
	 * @param userDTO 用户电话信息
	 * @return 返回查询的用户
	 */
	@PostMapping("/administrator/user/assigned")
	public String getUserByTelephone(@RequestBody UserDTO userDTO) {
		return this.reUserStateInfo(userDTO);
	}

	/**
	 * 根据条件分页查询
	 *
	 * @param userDTO 查询条件
	 * @return 返回查询信息
	 */
	private String reUserStateInfo(UserDTO userDTO) {
		Map<String, Object> message = new HashMap<>(16);
		Page<User> page = userService.getUserByConditionPage(userDTO);
		message.put("message", page.getRecords());
		message.put("total", page.getTotal());
		return JSON.toJSONString(message);
	}

	/**
	 * 查询所有商家
	 *
	 * @param currentPage 页号
	 * @param pageSize    每页数量
	 * @return 返回查询结果
	 */
	@PostMapping(value = "/administrator/merchant/pagination", produces = "application/json;charset=utf-8")
	public String getAllMerchant(Integer currentPage, Integer pageSize) {
		UserDTO userDTO = new UserDTO();
		userDTO.setIdentity(1).setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reUserStateInfo(userDTO);
	}

	/**
	 * 修改商家的等级
	 *
	 * @param id   商家id
	 * @param rate 商家等级
	 * @return 返回修改结果
	 */
	@PostMapping(value = "/administrator/merchant/update", produces = "application/json;charset=utf-8")
	public String updateMerchantRate(Integer id, Integer rate) {
		Map<String, Object> message = new HashMap<>(16);
		User merchant = userService.getById(id);
		merchant.setRate(rate);
		boolean b = userService.updateById(merchant);
		message.put("information", b ? 1 : 0);
		return JSON.toJSONString(message);
	}

	/**
	 * 根据等级查询商家
	 *
	 * @param rate        商家等级
	 * @param currentPage 页号
	 * @param pageSize    每页数量
	 * @return 返回查询结果
	 */
	@PostMapping(value = "/administrator/merchant/assigned", produces = "application/json;charset=utf-8")
	public String getMerchantByRate(Integer rate, Integer currentPage, Integer pageSize) {
		UserDTO userDTO = new UserDTO();
		userDTO.setIdentity(1).setRate(rate).setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reUserStateInfo(userDTO);
	}

}
